package com.sg.business.work.view;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.bizvision.widgets.dhtmlscheduler.DhtmlxScheduler;
import com.bizvision.widgets.dhtmlscheduler.Schedule;
import com.bizvision.widgets.dhtmlscheduler.ScheduleChart;
import com.mobnut.db.DBActivator;
import com.mobnut.db.model.IPrimaryObjectLabelProvider;
import com.mobnut.db.model.ModelService;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.Work;

public class WorkCalendar extends ViewPart {

	private DhtmlxScheduler chart;
	private DBCollection col;

	public WorkCalendar() {
	}

	@Override
	public void createPartControl(Composite parent) {
		chart = new DhtmlxScheduler(parent, SWT.NONE);
		col = DBActivator.getCollection(IModelConstants.DB,
				IModelConstants.C_WORK);
	}

	public void setInput(DBObject queryContidtion) {
		ScheduleChart scheduleChart = new ScheduleChart();
		DBCursor cursor = col.find(queryContidtion);
		String text;
		Date start;
		Date end;
		String style;
		while (cursor.hasNext()) {
			DBObject data = cursor.next();
			Work work = ModelService.createModelObject(data, Work.class);
			IPrimaryObjectLabelProvider lp = work.getLabelprovider();
			if(lp!=null){
				text = lp.getExtendLabel(work);
			}else{
				text = work.getLabel();
			}
			start = work.getPlanStart();
			end = work.getPlanFinish();
			if(start == null || end == null){
				continue;
			}

			
			if(work.getActualFinish()!=null){
				style = Schedule.SUBJECT_NORMAL;
			}else{
				if(work.isDelayFinish()){
					style = Schedule.SUBJECT_ALERT;
				}else if(work.isDelayStart()){
					style = Schedule.SUBJECT_WARNING;
				}else{
					style = Schedule.SUBJECT_INFORMATION;
				}
			}
			
			Schedule schedule = new Schedule(work.get_id().toString(), text, start, end,style);
			
			scheduleChart.addSchedule(schedule);
		}
		chart.setScheduleChart(scheduleChart);
		cursor.close();
	}

	@Override
	public void setFocus() {

	}

}
